package com.draven.mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.draven.mall.entity.MerchantApplication;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 商家入驻申请 Mapper 接口
 */
@Mapper
public interface MerchantApplicationMapper extends BaseMapper<MerchantApplication> {

    /**
     * 获取待审核的商家列表
     * @return 待审核的商家列表
     */
    @Select("SELECT * FROM tb_merchant_application WHERE status = 0")
    List<MerchantApplication> selectPendingMerchants();

    /**
     * 根据关键词搜索商家（分页）
     * @param page 分页参数
     * @param keyword 搜索关键词
     * @return 分页结果
     */
    @Select("<script>" +
            "SELECT * FROM tb_merchant_application " +
            "<where>" +
            "<if test='keyword != null and keyword != \"\"'>" +
            "  store_name LIKE CONCAT('%', #{keyword}, '%') " +
            "  OR contact LIKE CONCAT('%', #{keyword}, '%') " +
            "  OR phone LIKE CONCAT('%', #{keyword}, '%') " +
            "</if>" +
            "</where>" +
            "ORDER BY created_time DESC" +
            "</script>")
    IPage<MerchantApplication> selectMerchantsByKeyword(Page<MerchantApplication> page, @Param("keyword") String keyword);

    /**
     * 更新商家审核状态
     * @param id 商家ID
     * @param status 审核状态
     * @param rejectReason 拒绝原因
     * @param auditorId 审核人ID
     * @return 更新结果
     */
    @Update("UPDATE tb_merchant_application SET status = #{status}, " +
            "reject_reason = #{rejectReason}, auditor_id = #{auditorId}, " +
            "audit_time = NOW(), updated_time = NOW() " +
            "WHERE id = #{id}")
    int updateMerchantStatus(@Param("id") Long id, 
                             @Param("status") Integer status, 
                             @Param("rejectReason") String rejectReason, 
                             @Param("auditorId") Long auditorId);
} 